﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApi.DbContexts;
using WebApi.Entities;

namespace WebApi.Controllers;

[ApiController]
[Route("[controller]")]
public class UserController :Controller
{
    public UserController()
    {

    }


    [HttpPost]
    [Route("add")]
    public ActionResult<int> AddUser()
    {
        var context = new AppDbContext();

        var entity = new User()
        {
            Id = Guid.NewGuid(),
            Name = "test",
            Address = new Address()
            {
                City = "shenzhen",
                
            }
        };
              

        context.Add(entity);
        return context.SaveChanges();

    }

    [HttpPut]
    [Route("addphone")]
    public ActionResult<int> UpdateUser()
    {
        var context = new AppDbContext();

        var user = context.User.Where(x => x.Address.City == "shenzhen").FirstOrDefault();

        if (user == null) throw new Exception("not found");

        user.Address.Phones.Add(new Phone { Number = "18600125456585" });

        return context.SaveChanges();
    }



    [HttpPut]
    [Route("removephone")]
    public ActionResult<int> DeleteUser()
    {
        var context = new AppDbContext();

        var user = context.User.Where(x => x.Address.City == "shenzhen").FirstOrDefault();

        if (user == null) throw new Exception("not found");

        var phone = user.Address.Phones.First();

        if (phone!=null)
            user.Address.Phones.Remove(phone);

        return context.SaveChanges();
    }
}
